#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<list>
#include<algorithm>
using namespace std;

#include "string.h"

namespace ltw
{
	void test_string1()
	{
		ltw::string s1("hello world");
		cout << s1.c_str() << endl;

		for (size_t i = 0; i < s1.size(); i++)
		{
			s1[i]++;
		}

		for (size_t i = 0; i < s1.size(); i++)
		{
			cout << s1[i] << " ";
		}
		cout << endl;

		// 封装：屏蔽了底层实现细节，提供了一种简单通用访问容器的方式
		// 支付宝和微信的支付
		// 网银
		string::iterator it1 = s1.begin();
		while (it1 != s1.end())
		{
			cout << *it1 << " ";
			++it1;
		}
		cout<<endl;

		for (auto e : s1)
		{
			cout << e << " ";
		}
		cout << endl;

		ltw::string s2;
		cout << s2.c_str() << endl;

		const ltw::string s3("xxxxxxx");

		string::const_iterator it3 = s3.begin();
		while (it3 != s3.end())
		{
			// *it3 = 'y';

			cout << *it3 << " ";
			++it3;
		}
		cout << endl;

		for (size_t i = 0; i < s3.size(); i++)
		{
			//s3[i]++;
			cout << s3[i] << " ";
		}
		cout << endl;
	}

    

	void test_string2()
	{
		ltw::string s1("hello world");
		cout << s1.c_str() << endl;

		s1.push_back('x');
		cout << s1.c_str() << endl;

		s1.append("yyyyy");
		cout << s1.c_str() << endl;

		s1 += 'z';
		s1 += "密码密密麻麻";
		cout << s1.c_str() << endl;
	}

	void test_string3()
	{
		ltw::string s1("hello world");
		cout << s1.c_str() << endl;

		s1.insert(6, 'x');
		cout << s1.c_str() << endl;

		s1.insert(0, 'x');
		cout << s1.c_str() << endl;

		ltw::string s2("hello world");
		cout << s2.c_str() << endl;

		s2.insert(6, "yyy");
		cout << s2.c_str() << endl;

		s2.insert(0, "yyy");
		cout << s2.c_str() << endl;

		ltw::string s3("hello world");
		cout << s3.c_str() << endl;
		//s3.erase(6, 10);
		s3.erase(6);
		cout << s3.c_str() << endl;

		ltw::string s4("hello world");
		cout << s4.c_str() << endl;
		s4.erase(6, 3);
		cout << s4.c_str() << endl;
	}

	void test_string4()
	{
		ltw::string s1("hello world");
		cout << s1.find('o') << endl;
		cout << s1.find("wor") << endl;
	}

	void test_string5()
	{
		ltw::string s1("hello world");
		ltw::string s2(s1);

		s1[0] = 'x';
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		ltw::string s3("yyyy");
		s1 = s3;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		ltw::string s4("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
		s1 = s4;
		cout << s1.c_str() << endl;
		cout << s4.c_str() << endl;

		s1 = s1;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		std::swap(s1, s3);
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		s1.swap(s3);
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;
	}


	void test_string6()
	{
		ltw::string url("https://gitee.com/ailiangshilove/cpp-class/blob/master/%E8%AF%BE%E4%BB%B6%E4%BB%A3%E7%A0%81/C++%E8%AF%BE%E4%BB%B6V6/string%E7%9A%84%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95%E5%8F%8A%E4%BD%BF%E7%94%A8/TestString.cpp");
		size_t pos1 = url.find(':');
		ltw::string url1 = url.substr(0, pos1 - 0);
		cout << url1.c_str() << endl;

		size_t pos2 = url.find('/', pos1 + 3);
		ltw::string url2 = url.substr(pos1 + 3, pos2 - (pos1 + 3));
		cout << url2.c_str() << endl;

		ltw::string url3 = url.substr(pos2 + 1);
		cout << url3.c_str() << endl;
	}

	void test_string7()
	{
		ltw::string s1("hello world");
		cout << s1 << endl;

		cin >> s1;
		cout << s1 << endl;
	}
}

int main()
{
	ltw::test_string7();

	return 0;
}